package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.DeviceArchivesFileMapper;
import com.zhijian.medical.entity.po.DeviceArchivesFile;
import com.zhijian.medical.entity.vo.request.DeviceArchivesFileSearchVo;
import com.zhijian.medical.entity.vo.response.DeviceArchivesFileVo;
import com.zhijian.medical.service.DeviceArchivesFileService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 档案文件业务处理层
 *
 * @author HCG
 * @date 2022-01-01 15:38:53
 * @version: 1.0
 */
@Slf4j
@Service
public class DeviceArchivesFileServiceImpl implements DeviceArchivesFileService {

    @Autowired
    private DeviceArchivesFileMapper mapper;

    @Override
    public DeviceArchivesFile selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DeviceArchivesFileVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(DeviceArchivesFile deviceArchivesFile) {
        Date now = new Date();
        deviceArchivesFile.setCreateTime(now);
        deviceArchivesFile.setUpdateTime(now);
        return mapper.insert(deviceArchivesFile) == 1 ? deviceArchivesFile.getId() : 0L;
    }

    @Override
    public boolean update(DeviceArchivesFile deviceArchivesFile) {
        Date now = new Date();
        deviceArchivesFile.setUpdateTime(now);
        return mapper.updateByPrimaryKey(deviceArchivesFile) != 0;
    }

    @Override
    public Integer count(DeviceArchivesFileSearchVo deviceArchivesFileSearchVo) {
        return mapper.count(deviceArchivesFileSearchVo);
    }

    @Override
    public List<DeviceArchivesFileVo> selectList(DeviceArchivesFileSearchVo deviceArchivesFileSearchVo) {
        return mapper.selectList(deviceArchivesFileSearchVo);
    }

    @Override
    public Page<DeviceArchivesFileVo> selectPage(DeviceArchivesFileSearchVo deviceArchivesFileSearchVo) {
        //先直接查总数
        int count = count(deviceArchivesFileSearchVo);
        //分页
        Page<DeviceArchivesFileVo> page = new Page<>(true, count, deviceArchivesFileSearchVo.getPageNum(), deviceArchivesFileSearchVo.getPageSize());
        deviceArchivesFileSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DeviceArchivesFileVo> responseVos = this.selectList(deviceArchivesFileSearchVo);
        page.setData(responseVos);
        return page;
    }

}